#include"pgmfc_c.h"

#include <stdio.h>
#include <Windows.h>
#include <string>
#include <iostream>
#include <process.h>

using namespace std;

typedef unsigned char(*dll_detect)(unsigned short ports[20]);
typedef unsigned char(*dlltz_detect)(unsigned short ports[10]);

typedef unsigned long long(*dll_initialize)(unsigned short nPort);
typedef unsigned long long(*dlltz_initialize)(unsigned short nPort);
typedef unsigned char(*dll_close)(unsigned long long handle);

struct Native {
	HMODULE hDll = NULL;
	dll_detect detect;
	dlltz_detect tz_detetct;

	dll_initialize initialize;
	dlltz_initialize tz_initialize;

	dll_close close;
};
static Native native;

int PGMFC_Native::init() {
	if (native.hDll != NULL) {
		return true;
	}
	native.hDll = LoadLibrary(L"pgmfc_64.dll");

	if (!native.hDll)
	{
		std::cout << "PGMFC dll load failed" << std::endl;
		return 0;
	}

	native.detect = (dll_detect)GetProcAddress(native.hDll, "mfcs_detect");
	native.initialize = (dll_initialize)GetProcAddress(native.hDll, "mfcs_initialization");

	native.close = (dll_close)GetProcAddress(native.hDll, "mfcs_close");
	return 1;
}

void PGMFC_Native::release() {
	FreeLibrary(native.hDll);
}

void PGMFC_Native::test() {
	unsigned long long handle = PGMFC_Native::mfcs_initialization(7);
	if (handle == 0) {
		cout << "NULL handle" << endl;
	}
	else {
		cout << "Success" << endl;
		PGMFC_Native::mfcs_close(handle);
	}
}

unsigned char PGMFC_Native::mfcs_detect(unsigned short ports[20]) {
	return native.detect(ports);
}

unsigned long long PGMFC_Native::mfcs_initialization(unsigned short nPort) {
	return native.initialize(nPort);
}

unsigned char PGMFC_Native::mfcs_close(unsigned long long handle) {
	return native.close(handle);
}
